<template>
  <div class="app-container">
    <el-row :gutter="20" class="header-tip">
      <el-col :span="24">
        <el-alert
          title="注意：本模块仅显示已通过体能测评的考生"
          type="info"
          show-icon
          :closable="false">
        </el-alert>
      </el-col>
    </el-row>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="报名序号" prop="registrationId">
        <el-input
          v-model="queryParams.registrationId"
          placeholder="请输入报名序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生源地市" prop="nativeCity">
        <el-input
          v-model="queryParams.nativeCity"
          placeholder="请输入生源地市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="准考证号(纸笔)" prop="writtenExamNo">
        <el-input
          v-model="queryParams.writtenExamNo"
          placeholder="请输入准考证号(纸笔)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="准考证号(机考)" prop="computerExamNo">
        <el-input
          v-model="queryParams.computerExamNo"
          placeholder="请输入准考证号(机考)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体检面试结果" prop="medicalInterview">
        <el-select v-model="queryParams.medicalInterview" placeholder="请选择体检面试结果" clearable>
          <el-option
            v-for="dict in sulectList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:medical:add']"
        >新增</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:medical:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:medical:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:medical:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:medical:import']"
        >导入</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImportTemplate"
          v-hasPermi="['system:medical:import']"
        >下载模板</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-view"
          size="mini"
          @click="clickIdCard"
        >识别</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleBatchPass"
          v-hasPermi="['system:qualification:edit']"
        >批量体检面试通过</el-button>
      </el-col>
      <!-- 新增全选按钮 -->
<!--      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-check"
          size="mini"
          @click="toggleAllSelection"
        >全选</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medicalList" @selection-change="handleSelectionChange" ref="qualificationTable">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="自增主键" align="center" prop="id" />-->
      <el-table-column label="报名序号" align="center" prop="registrationId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="生源地市" align="center" prop="nativeCity" />
      <el-table-column label="准考证号(纸笔)" align="center" prop="writtenExamNo" />
      <el-table-column label="准考证号(机考)" align="center" prop="computerExamNo" />
      <el-table-column label="行测成绩" align="center" prop="administrativeScore" />
      <el-table-column label="申论成绩" align="center" prop="essayScore" />
      <el-table-column label="专业科目成绩" align="center" prop="professionalScore" />
      <el-table-column label="总成绩" align="center" prop="totalScore" />
      <el-table-column label="合成分数" align="center" prop="compositeScore" />
      <el-table-column label="面试成绩" align="center" prop="interviewScore" />
      <el-table-column label="标记" align="center" prop="remark" />
      <el-table-column label="行测缺考" align="center" prop="administrativeAbsent" />
      <el-table-column label="申论缺考" align="center" prop="essayAbsent" />
      <el-table-column label="专业科目缺考" align="center" prop="professionalAbsent" />
      <el-table-column label="政治面貌" align="center" prop="politicalStatus" />
      <el-table-column label="最高学历" align="center" prop="highestDegree" />
      <el-table-column label="学位" align="center" prop="academicDegree" />
      <el-table-column label="毕业院校" align="center" prop="graduateSchool" />
      <el-table-column label="院系" align="center" prop="department" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="体检面试结果" align="center" prop="medicalInterview" width=”150”>
        <template slot-scope="scope">
          <el-tag :type="scope.row.medicalInterview == '未通过' ? 'danger' : 'success'">{{scope.row.medicalInterview}}</el-tag>
<!--          <dict-tag :options="dict.type.sys_medical_status" :value="scope.row.medicalInterview"/>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:medical:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:medical:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改警校考生体检面试信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-alert
          title="提示：当体检面试结果设置为 通过 时，考生信息将自动流转到职位选择模块"
          type="info"
          :closable="false"
          style="margin-bottom: 15px;"
        />
        <el-alert
          title="提示：当资格审查、体能测评、体检面试三个环节均通过时，最终结果将自动设置为 通过 "
          type="success"
          :closable="false"
          style="margin-bottom: 15px;"
        />
        <el-row>
          <el-col :span="12">
            <el-form-item label="报名序号" prop="registrationId">
              <el-input v-model="form.registrationId" placeholder="请输入报名序号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-input v-model="form.gender" placeholder="请输入性别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生源地市" prop="nativeCity">
              <el-input v-model="form.nativeCity" placeholder="请输入生源地市" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="准考证号(纸笔)" prop="writtenExamNo">
              <el-input v-model="form.writtenExamNo" placeholder="请输入准考证号(纸笔)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="准考证号(机考)" prop="computerExamNo">
              <el-input v-model="form.computerExamNo" placeholder="请输入准考证号(机考)" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">
            <el-form-item label="行测成绩" prop="administrativeScore">
              <el-input v-model="form.administrativeScore" placeholder="请输入行测成绩" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="申论成绩" prop="essayScore">
              <el-input v-model="form.essayScore" placeholder="请输入申论成绩" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="专业科目成绩" prop="professionalScore">
              <el-input v-model="form.professionalScore" placeholder="请输入专业科目成绩" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="总成绩" prop="totalScore">
              <el-input v-model="form.totalScore" placeholder="请输入总成绩" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="合成分数" prop="compositeScore">
              <el-input v-model="form.compositeScore" placeholder="请输入合成分数" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="面试成绩" prop="interviewScore">
              <el-input v-model="form.interviewScore" placeholder="请输入面试成绩" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="标记" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入标记" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="行测缺考" prop="administrativeAbsent">
              <el-input v-model="form.administrativeAbsent" placeholder="请输入行测缺考" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="申论缺考" prop="essayAbsent">
              <el-input v-model="form.essayAbsent" placeholder="请输入申论缺考" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="专业科目缺考" prop="professionalAbsent">
              <el-input v-model="form.professionalAbsent" placeholder="请输入专业科目缺考" />
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="最高学历" prop="highestDegree">
              <el-input v-model="form.highestDegree" placeholder="请输入最高学历" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学位" prop="academicDegree">
              <el-input v-model="form.academicDegree" placeholder="请输入学位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业院校" prop="graduateSchool">
              <el-input v-model="form.graduateSchool" placeholder="请输入毕业院校" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="院系" prop="department">
              <el-input v-model="form.department" placeholder="请输入院系" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专业" prop="major">
              <el-input v-model="form.major" placeholder="请输入专业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phoneNumber">
              <el-input v-model="form.phoneNumber" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">
            <el-form-item label="资格审查结果" prop="qualificationReview">
              <el-input v-model="form.qualificationReview" placeholder="请输入资格审查结果" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="体能测评结果" prop="physicalTest">
              <el-input v-model="form.physicalTest" placeholder="请输入体能测评结果" />
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="体检面试结果" prop="medicalInterview">
              <el-radio v-model="form.medicalInterview" label="通过">通过</el-radio>
              <el-radio v-model="form.medicalInterview" label="未通过">未通过</el-radio>
<!--              <el-select v-model="form.medicalInterview" placeholder="请选择体检面试结果"  style="width: 100%">-->
<!--                <el-option-->
<!--                  v-for="dict in dict.type.sys_medical_status"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.label"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">
            <el-form-item label="最终结果" prop="result">
              <el-input v-model="form.result" placeholder="请输入最终结果" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="报考职位" prop="positionChoice">
              <el-input v-model="form.positionChoice" placeholder="请输入报考职位" />
            </el-form-item>
          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：如果数据已存在，将根据身份证号或报名序号更新数据</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import {listMedical, getMedical, delMedical, addMedical, updateMedical, batchPassMedical} from "@/api/system/medical";
import { getToken } from "@/utils/auth";
import {listPhysical} from "@/api/system/physical";

export default {
  name: "Medical",
  dicts: ['sys_medical_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 警校考生体检面试信息表格数据
      medicalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        registrationId: null,
        name: null,
        idCard: null,
        nativeCity: null,
        writtenExamNo: null,
        computerExamNo: null,
        phoneNumber: null,
        medicalInterview: null,
        physicalTest: '通过'
      },
      // 表单参数
      form: {},
      sulectList: [{
        value: '通过',
        label: '通过'
      },{
      value: '未通过',
        label: '未通过'
    }],
      // 表单校验
      rules: {
        registrationId: [
          { required: true, message: "报名序号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
      },
      // 上传参数
      upload: {
        // 是否显示导入对话框
        open: false,
        // 导入对话框标题
        title: "导入警校考生体检面试信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/medical/importData"
      },
      selectedRows: [], // 新增字段，用于存储选中的行数据

    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    clickIdCard() {
      axios.get('http://127.0.0.1:19196/readcard')
        .then((res) => {
          let idCard = res.data.certNumber
          this.queryParams.pageNum = 1;
          this.queryParams.idCard = idCard;
          this.loading = true;
          listMedical(this.queryParams).then(response => {
            this.listMedical = response.rows;
            this.total = response.total;
            this.loading = false;
            // 检查结果数量是否为1，如果是则弹出修改窗口
            if (this.listMedical.length === 1) {
              this.handleUpdate(this.listMedical[0]);
            }else{
              this.$message.error('未查询到此人');
            }
            this.queryParams = {}
            this.getList()
          });
        })
    },
    /** 查询警校考生体检面试信息列表 */
    getList() {
      this.loading = true;
      listMedical(this.queryParams).then(response => {
        this.medicalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        registrationId: null,
        name: null,
        idCard: null,
        gender: null,
        nativeCity: null,
        writtenExamNo: null,
        computerExamNo: null,
        administrativeScore: null,
        essayScore: null,
        professionalScore: null,
        totalScore: null,
        compositeScore: null,
        interviewScore: null,
        remark: null,
        administrativeAbsent: null,
        essayAbsent: null,
        professionalAbsent: null,
        politicalStatus: null,
        highestDegree: null,
        academicDegree: null,
        graduateSchool: null,
        department: null,
        major: null,
        phoneNumber: null,
        qualificationReview: null,
        physicalTest: null,
        medicalInterview: null,
        result: null,
        positionChoice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selectedRows = selection; // 更新选中的行数据
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加警校考生体检面试信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMedical(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改警校考生体检面试信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMedical(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMedical(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除警校考生体检面试信息编号为"' + ids + '"的数据项？').then(function() {
        return delMedical(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/medical/export', {
        ...this.queryParams
      }, `medical_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "警校考生体检面试信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    handleImportTemplate() {
      this.download('/system/medical/importTemplate', {}, `警校考生体检面试信息_导入模板.xlsx`);
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 新增方法，处理批量通过逻辑
    handleBatchPass() {
      if (this.selectedRows.length === 0) {
        this.$modal.msgWarning("请先选择需要通过的记录");
        return;
      }

      this.$modal.confirm('是否确认将选中的记录体检面试结果批量设置为通过？').then(() => {
        // 设置选中的记录的资格审查结果为“通过”
        const updatedRows = this.selectedRows.map(row => ({
          ...row,
          medicalInterview: '通过'
        }));

        batchPassMedical(updatedRows).then(response => {
          this.$modal.msgSuccess("批量通过成功");
          this.getList();
        }).catch(() => {});
      }).catch(() => {});
    },

    // 新增方法，用于全选表格中的所有行
    toggleAllSelection() {
      this.$refs.qualificationTable.toggleAllSelection();
    },
  }
};
</script>

<style scoped>
.header-tip {
  margin-bottom: 20px;
}
</style>
